package com.ln.asset.core.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.Transformation;
import com.ln.asset.core.service.TransformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qiaoyanru
 * @since 2022-04-27
 */
@Api(tags = "资产转移管理")
@RestController
@RequestMapping("/admin/core/transformation")
@Slf4j
public class AdminTransformationController {
    @Resource
    private TransformationService transformationService;

    @ApiOperation("转移资产信息")
    @GetMapping("/getPageList/{page}/{limit}")
    public R getTransformation(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<Transformation> pageParam = new Page<>(page,limit);
        IPage<Transformation> list = transformationService.selectList(pageParam);

        return R.ok().data("list",list);
    }

    @ApiOperation("删除转移资产信息")
    @DeleteMapping("/delTransfor/{id}")
    public R delTransfor(
            @ApiParam(value = "id",required = true) @PathVariable  Integer id){
        try {
            transformationService.delTransfor(id);
            return R.ok().message("删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("转移资产详情")
    @GetMapping("/transforDetails/{id}")
    public R transforDetails(@ApiParam(value = "id",required = true)
                                         @PathVariable Integer id){
        Map<String,Object> map = transformationService.selectDetails(id);
        return R.ok().data("transforDetails",map);
    }

    @ApiOperation("资产转移审批")
    @PostMapping("/tansforApproval")
    public R transforApproval(@RequestParam(name = "status",required = false) Integer status,
                                  @RequestBody Map<String,Object> transforDetails){
        try {
            transformationService.transforApproval(status,transforDetails);
            return R.ok().message("审批通过");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("库存不足");
        }
    }
}

